package com.jdj.framework.core.base.vo;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页对象
 * @author caijinbang
 * @date 2019-04-13 13:46
 */
public class PageInfo<T> implements Serializable {

    private static final long serialVersionUID = -4315304098012695119L;

    /**
     * 每页显示记录数
     */
    private int pageSize = 10;

    /**
     * 查询结果总记录数
     */
    private int totalRecord = 0;

    /**
     * 当前页面
     */
    private int currentPage = 1;

    /**
     * 总共页数
     */
    private int totalPage;

    /**
     * 记录集
     */
    private List<T> records = Collections.emptyList();

    public PageInfo() {
    }

    /**
     * @param currentPage 当前页码
     * @param pageSize 每页显示记录数
     */
    public PageInfo(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    /**
     * @param currentPage 当前页码
     * @param totalRecord 总记录数
     * @param pageSize 每页显示记录数
     */
    public PageInfo(int currentPage, int totalRecord, int pageSize) {
        this.currentPage = currentPage;
        this.totalRecord = totalRecord;
        this.pageSize = pageSize;
    }

    /**
     * 开始记录位置
     *
     * @return the recordStart
     */
    public int getRecordStart() {
        if (this.currentPage > 0) {
            return (this.currentPage - 1) * this.pageSize + 1;
        } else {
            return 0;
        }
    }

    /**
     * 开始记录位置(前一行，解决数据库分页时的差异， 0 或 1 作为第一行记录)
     *
     * @return the recordStart
     */
    public int getRecordStartPrev() {
        if (this.currentPage > 0) {
            return (this.currentPage - 1) * this.pageSize;
        } else {
            return 0;
        }
    }

    /**
     * 结束记录位置
     *
     * @return the recordEnd
     */
    public int getRecordEnd() {
        if (this.currentPage > 0) {
            return this.currentPage * this.pageSize;
        } else {
            return 0;
        }
    }

    /**
     * 每页显示记录数
     *
     * @return the pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 每页显示记录数
     *
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取总记录数
     *
     * @return the totalRecord
     */
    public int getTotalRecord() {
        return totalRecord;
    }

    /**
     * 设置总记录数
     *
     * @param totalRecord the totalRecord to set
     */
    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    /**
     * 当前页码
     *
     * @return the currentPage
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * 当前页码
     *
     * @param currentPage the currentPage to set
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * 总共页数
     *
     * @return the totalPage
     */
    public int getTotalPage() {
        this.totalPage = (int) Math.floor((this.totalRecord * 1.0d) / this.pageSize);
        if (this.totalRecord % this.pageSize != 0) {
            this.totalPage++;
        }
        if (totalPage == 0) {
            return 1;
        }
        return totalPage;
    }

    /**
     * 获取记录集
     *
     * @return 记录集
     */
    public List<T> getRecords() {
        return records;
    }

    /**
     * 设置记录集
     *
     * @param records 记录集
     */
    public void setRecords(List<T> records) {
        this.records = records;
    }

}
